package com.mobile.check.fragments;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.base.BaseListFragment;
import cn.droidlover.xdroidmvp.base.BaseRecyclerAdapter;
import cn.droidlover.xdroidmvp.base.ListItemLongClickListener;
import cn.droidlover.xdroidmvp.base.RecyclerViewHolder;
import cn.droidlover.xdroidmvp.cache.SharedPref;
import cn.droidlover.xdroidmvp.kit.NToast;
import com.mobile.check.activitys.InputEditActivity;
import com.mobile.check.activitys.TaskItemListActivity;
import com.mobile.check.beans.TaskInfo;
import com.mobile.check.app.R;
import com.mobile.check.db.EntityManager;
import com.mobile.check.util.AppContents;
import com.mobile.check.dialogs.SubmitTipDialog;
import com.mobile.check.present.JiancePresent;

@Deprecated
public class JianceFragment extends BaseListFragment<TaskInfo, JiancePresent> {

    boolean isEditModel;
    @BindView(R.id.leftIv)
    ImageView leftIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.rightTv)
    TextView rightTv;
    @BindView(R.id.submitTv)
    Button submitTv;
    private int taskType;//0 检测任务  1 复检任务 3 跟踪任务
    private List<TaskInfo> selectList = new ArrayList<>();


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        taskType = getArguments().getInt("taskType", 0);
    }

    @Override
    public void initView() {
        super.initView();
        leftIv.setImageResource(R.drawable.setting);
        leftIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputEditActivity.lunch(getActivity());
            }
        });

        switch (taskType) {
            case 0:
                titleTv.setText("检查任务");
                break;
            case 1:
                titleTv.setText("复检任务");
                break;
            case 2:
                titleTv.setText("跟踪任务");
                break;
        }
        rightTv.setText("导入");
        rightTv.setVisibility(View.VISIBLE);
        mBaseAdapter.setLongClickListener(new ListItemLongClickListener() {
            @Override
            public void itemLongClick(int position) {
                isEditModel = true;
                rightTv.setText("取消");
            }
        });
    }


    @Override
    public BaseRecyclerAdapter<TaskInfo> getAdapter() {
        return new BaseRecyclerAdapter<TaskInfo>(getActivity()) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.item_task;
            }

            @Override
            public void bindData(RecyclerViewHolder holder, int position) {
                super.bindData(holder, position);
                TaskInfo info = getItemByPosition(position);
                holder.setText(R.id.deviceNameTv, info.getTask_name());
                if (info.isLocalFile()) {
                    holder.setVisibility(R.id.statusTv, View.VISIBLE);
                    holder.setText(R.id.statusTv, "本地任务");
                } else {
                    holder.setText(R.id.statusTv, "已上传");
                    if (info.isHadUploaded()) {
                        holder.setVisibility(R.id.statusTv, View.VISIBLE);
                    } else {
                        holder.setVisibility(R.id.statusTv, View.INVISIBLE);
                    }
                }
                if (isEditModel) {
                    holder.setVisibility(R.id.selectIv, View.VISIBLE);
                    if (selectList.contains(info)) {
                        holder.setImageResDrawable(R.id.selectIv, R.drawable.select1);
                    } else {
                        holder.setImageResDrawable(R.id.selectIv, R.drawable.select0);
                    }
                } else {
                    holder.setImageResDrawable(R.id.selectIv, R.drawable.taskicon);
                }
            }
        };
    }

    @Override
    public void itemClick(int position) {
        TaskInfo info = mBaseAdapter.getItemByPosition(position);
        if (isEditModel) {
            if (selectList.contains(info)) {
                selectList.remove(info);
            } else {
                selectList.add(info);
            }
            checkSlelectCount();
        } else {
            checkUserAndDevice(info);
        }
    }


    private void checkUserAndDevice(final TaskInfo info) {
        String userName = SharedPref.getInstance(getActivity()).getString(AppContents.USERNAME, "");
        String device = SharedPref.getInstance(getActivity()).getString(AppContents.TESTDEVICE, "");
        if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(device)) {
            SubmitTipDialog submitTipDialog = new SubmitTipDialog(getActivity(), new SubmitTipDialog.SlectClickListener() {
                @Override
                public void submitClick() {
                    InputEditActivity.lunch(getActivity());
                }

                @Override
                public void cancelClick() {

                }
            });
            submitTipDialog.display("温馨提示", "请完善当前检测人和检测设别后进行。是否设置？", "取消", "设置");
        } else {
            SubmitTipDialog submitTipDialog = new SubmitTipDialog(getActivity(), new SubmitTipDialog.SlectClickListener() {
                @Override
                public void submitClick() {
                    Intent intent = new Intent(getActivity(), TaskItemListActivity.class);
                    intent.putExtra("taskInfo", info);
                    startActivityForResult(intent, 0x001);
                }

                @Override
                public void cancelClick() {
                    InputEditActivity.lunch(getActivity());
                }
            });
            submitTipDialog.display("温馨提示", "当前检测人员：" + userName + "\n" + "当前检测设备：" + device, "修改", "继续");
        }
    }


    @Override
    public int getContentViewId() {
        return R.layout.fragment_tasklist;
    }

    @Override
    public void getDataFromServer() {
        getP().queryTaskInfo(taskType);
    }

    @Override
    public JiancePresent newP() {
        return new JiancePresent();
    }


    @OnClick({R.id.rightTv, R.id.submitTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rightTv:
//                if (isEditModel) {
//                    selectList.clear();
//                    isEditModel = false;
//                    submitTv.setVisibility(View.INVISIBLE);
//                    mBaseAdapter.notifyDataSetChanged();
//                    rightTv.setText("导入");
//                } else {
//                    showPhotoBottomDialog();
//                }
                break;
            case R.id.submitTv:
                if (selectList.size() == 0) {
                    NToast.shortToast(getActivity(), "请选择检测项");
                } else {
                    EntityManager.getInstance().delTaskInfo(selectList);
                    mBaseAdapter.remove(selectList);
                    selectList.clear();
                    if (mBaseAdapter.getItemCount() == 0) {
                        isEditModel = false;
                        submitTv.setVisibility(View.GONE);
                        rightTv.setText("导入");
                    }
                }
                break;
        }
    }


    private void checkSlelectCount() {
        if (selectList.size() != 0) {
            submitTv.setVisibility(View.VISIBLE);
        } else {
            submitTv.setVisibility(View.GONE);
        }
        mBaseAdapter.notifyDataSetChanged();
    }

//    protected void showPhotoBottomDialog() {
//        List<String> list = new ArrayList<>();
//        list.add("本地导入");
//        list.add("网络导入");
//        BottomListDialog bottomListDialog = new BottomListDialog(getActivity(), new BottomListDialog.BottomListSelectListener() {
//            @Override
//            public void itemClickCallback(String selectStr) {
//                if (selectStr.equals("本地导入")) {
////                    LocalTaskListActivity.lunch(JianceFragment.this, taskType);
//                } else {
//                    TaskServerListActivity.lunch(getActivity(), taskType);
//                }
//            }
//        });
//        bottomListDialog.disPlay(list);
//    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == 0x001) {
            getDataFromServer();
        }
    }

    @Override
    public boolean isNotWorkConnect() {
        return false;
    }
}
